package com.example.demo.queue;

import com.example.demo.Service.UserService;
import com.example.demo.entity.User;
import com.rabbitmq.client.Channel;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.annotation.Exchange;
import org.springframework.amqp.rabbit.annotation.Queue;
import org.springframework.amqp.rabbit.annotation.QueueBinding;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;


import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.util.Date;

@Component
public class MessageListener {
    private UserService ce;
    @Autowired
    public void setCe(UserService ce) {
        this.ce = ce;
    }
    @RabbitListener(bindings = {
            @QueueBinding(
                    exchange=@Exchange(name = "spring.boot.delay.exchange",type = "direct"),
                    value = @Queue(name="spring.boot.rabbit.queue"),
                    key = {"spring.boot.rabbit.routingkey"}
            )
    })
    public void listen(String msg){
        System.out.println("收到消息："+msg);
    }


    @RabbitListener(queues = {"spring.boot.dlx.queue"})
    public void delay(Message message, Channel channel) throws IOException {
        String msg = new String(message.getBody());
        System.out.println(msg+"msg");
        Integer ms=Integer.parseInt(msg);
        Integer xiugai = ce.xiugai(ms, 1);
        System.out.println("收到延时队列的消息："+msg +":");
        channel.basicAck(message.getMessageProperties().getDeliveryTag(),false);
    }
}
